package com.itheima.bigevent.controller;

import com.itheima.bigevent.pojo.Result;
import com.itheima.bigevent.utils.AliyunOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        // 获取文件真实名称
        String originalFilename = file.getOriginalFilename();
        // 生成一个uuid并将上传文件的后缀拼接在后面当做新的文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + originalFilename.substring(originalFilename.lastIndexOf("."));
        // 将文件保存到本地
//        file.transferTo(new File("C:/Users/wuhaohua/Desktop/files/"+fileName));
        // 将文件上传至阿里云OSS
        String url = AliyunOssUtil.uploadFile(fileName, file.getInputStream());

        return Result.success(url);
    }
}
